const app = getApp()
// import trigger from '../../utils/util.js'
Page({
  data: {
    gao_on: '',
    isOver: false,
    scroll_true: '',
    isShow: '',
    current_type: '1',
    isShow: false
  },
  // 	guide_off:function(){
  // 		this.setData({
  // 			guide_on:''
  // 		})
  // 	},
  onLoad: function (options) {
    // (trigger.trigger)(this)
    app.updateFlag(this).then(res => {
      //公告
      app.request(app.apiDomain + "index.php?c=mall&a=notice", {
        type: 'activity'
      }, function (res) {
        // console.log(res)
        that.setData({
          gao: res.data.data
        });
      });
      // 获取活动列表数据
      wx.showLoading({
        title: '加载中',
      })
      app.request(app.apiDomain + 'index.php?c=activity&a=index', {
        page: 1,
        size: 20
      }, res => {
        wx.hideLoading()
        console.log(res);
        let signing = res.data.data.filter(item => {
          return item.status == 0
        })
        let doing = res.data.data.filter(item => {
          return item.status == 1
        })
        let ending = res.data.data.filter(item => {
          return item.status == 2
        })
        that.setData({
          list: res.data.data,
          sign: signing,
          doing: doing,
          ending: ending,
          signing_qi: signing.length <= 0 ? '' : signing[0].name,
          going_qi: doing.length <= 0 ? '' : doing[0].name
        })
        console.log('获取到的数据', that.data)
        if (that.data.sign.length > 0) {
          that.getDetail(that.data.sign)
          that.setData({
            open_date_1: that.data.sign.length > 0 ? that.data.sign[0].open_date.substring(5).replace('-', '.') : '',
            end_date_1: that.data.sign.length > 0 ? that.data.sign[0].end_date.substring(5).replace('-', '.') : ''
          })
        } else {
          that.setData({
            noData: true
          })
          // that.getDetail(that.data.doing)
        }
      })
    })
    console.log(options);
    var that = this;
    this.page_num = 1

    //第一次进来
    // 		if(options.new_url){
    // 			that.setData({
    // 				guide_on:'on',
    // 			})
    // 		}


    //授权
    // app.checkSession(this, '../activityList/promise', '', '', '', '', options.activity_id, '', true);
  },
  getDetail: function (option) {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    app.request(app.apiDomain + "index.php?c=activity&a=detail", {
      activity_id: option[0].id,
      page: 1,
      size: 10
    }, function (res) {
      wx.hideLoading()
      console.log(res)
      // 如果list数量没有size大,没必要加载
      if (res.data.data.list.length < 10) {
        that.scroll_suo = true
      } else {
        that.scroll_suo = false
      }
      // 判断当前活动状态
      var activity = res.data.data.activity
      if (activity.status == 0) {
        // 未开始
        if (res.data.data.user.length == 0) {
          // 未参加
          that.join = true
          var statusImg = '//resource.160nurse.com/canvas/healthy/img/activity/promise_1.png';
        } else {
          // 已参加
          that.join = false
          var statusImg = '//resource.160nurse.com/canvas/healthy/img/activity/promise_2.png';
        }
      } else if (activity.status == 1) {
        // 进行中
        that.join = false
        var statusImg = '//resource.160nurse.com/canvas/healthy/img/activity/promise_6.png';
      } else if (activity.status == 2) {
        // 结束
        that.join = false
        var statusImg = '//resource.160nurse.com/canvas/healthy/img/activity/promise_3.png';
        // 判断用户是否有守约
        if (res.data.data.user.status == '1') {
          // 成功
          var promiseImg = '//resource.160nurse.com/canvas/healthy/img/activity/promise_4.png'
        } else if (res.data.data.user.status == '2') {
          var promiseImg = '//resource.160nurse.com/canvas/healthy/img/activity/promise_5.png'
        }
        that.setData({
          isOver: true,
          promiseImg: promiseImg
        })
      }
      // var icon = res.data.data.activity.currency.substring(0,res.data.data.activity.currency.indexOf('.'))
      var icon = parseInt(res.data.data.activity.currency)
      that.setData({
        activity: activity,
        list: res.data.data.list,
        user: res.data.data.user,
        activity_id: option[0].id,
        statusImg: statusImg,
        icon: that.countIcon(icon.toString()),
        bail_currency: parseInt(activity.bail_currency),
        count: activity.count
      })
      if (that.data.current_type == '1') {
        that.setData({
          signing_qi: activity.name
        })
      } else if (option.length > 0) {
        that.setData({
          going_qi: activity.name
        })
      }
    });
  },
  // 刷新数据
  freshData: function () {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    app.request(app.apiDomain + 'index.php?c=activity&a=index', {
      page: 1,
      size: 20
    }, res => {
      wx.hideLoading({

      })
      console.log(res);
      let signing = res.data.data.filter(item => {
        return item.status == 0
      })
      let doing = res.data.data.filter(item => {
        return item.status == 1
      })
      let ending = res.data.data.filter(item => {
        return item.status == 2
      })
      that.setData({
        list: res.data.data,
        sign: signing,
        doing: doing,
        ending: ending,
        signing_qi: signing.length <= 0 ? '' : signing[0].name,
        going_qi: doing.length <= 0 ? '' : doing[0].name
      })
      if (that.data.sign.length > 0 && that.data.current_type == '1') {
        that.getDetail(that.data.sign)
        that.setData({
          noData: false
        })
      } else if (that.data.current_type == '2' && that.data.doing.length > 0) {
        that.getDetail(that.data.doing)
        that.setData({
          noData: false
        })
        that.setData({
          open_date: that.data.doing.length > 0 ? that.data.doing[0].open_date.substring(5).replace('-', '.') : '',
          end_date: that.data.doing.length > 0 ? that.data.doing[0].end_date.substring(5).replace('-', '.') : ''
        })
      } else if (that.data.current_type == '3' && that.data.ending.length > 0) {
        that.setData({
          noData: false
        })
      } else {
        that.setData({
          noData: true
        })
      }
    })
  },
  // 报名中
  getSigning:function() {
    var that = this
    that.setData({
      current_type: '1',
      page_num: 1
    })
    that.freshData()
  },
  // 进行中
  getDoing:function() {
    var that = this
    that.page_num = 1
    that.setData({
      current_type: '2',
      page_num: 1
    })
    that.freshData()
  },
  // 已结束
  getEnding: function () {
    var that = this
    var that = this
    that.setData({
      current_type: '3',
      page_num:1
    })
    wx.showLoading({
      title: '加载中',
    })
    if (that.data.ending.length > 0) {
      app.request(app.apiDomain + 'index.php?c=activity&a=index', {
        page: 1,
        size: 20
      }, res => {
        wx.hideLoading()
        console.log(res);
        let signing = res.data.data.filter(item => {
          return item.status == 0
        })
        let doing = res.data.data.filter(item => {
          return item.status == 1
        })
        let ending = res.data.data.filter(item => {
          return item.status == 2
        })
        that.setData({
          list: res.data.data,
          sign: signing,
          doing: doing,
          ending: ending,
          signing_qi: signing.length <= 0 ? '' : signing[0].name,
          going_qi: doing.length <= 0 ? '' : doing[0].name
        })
        if (ending.length < 10) {
          that.scroll_suo = true
        } else {
          that.scroll_suo = false
        }
      })
      // that.getDetail(that.data.ending)
      that.setData({
        noData: false
      })
    } else {
      that.setData({
        noData: true
      })
    }
  },
  authSuccess(e) {
    this.setData({
      isShow: false
    })
    this.freshData()
  },
  authFail(e) {
    this.setData({
      isShow: false
    })
  },
  click() {
    this.setData({
      isShow: false
    })
  },
  // getUserInfo: function () {
  //   var that = this
  //   if (app.getUserInfo(that)) {
  //     that.setData({
  //       user_auth: false
  //     })
  //   }
  // },
  getData: function () {

  },
  joinactivity() {
    var that = this
    // 参加活动
    if (!app.isAuth) {
      that.setData({
        isShow: true
      })
    } else {
      if (this.join) {
        // 提示扣币
        wx.showModal({
          title: '提示',
          content: '本次活动需支付' + that.data.activity.bail_currency + '健康币',
          success(res) {
            if (res.confirm) {
              // 改币 改图  改状态
              var icon = parseInt(that.data.activity.currency) + parseInt(that.data.activity.bail_currency)
              app.request(app.apiDomain + "index.php?c=activity&a=apply", {
                activity_id: that.data.activity_id
              }, res => {
                console.log(res);
                if (res.data.status == 1) {
                  // 报名成功
                  that.join = false
                  that.setData({
                    isShow: 'on',
                    user: res.data.data,
                    count: parseFloat(that.data.count) + 1,
                    statusImg: '//resource.160nurse.com/canvas/healthy/img/activity/promise_2.png'
                  })
                  setTimeout(() => {
                    that.setData({
                      icon: that.countIcon(icon.toString()),
                      isShow: ''
                    })
                  }, 800)
                } else {
                  wx.showToast({
                    title: res.data.info,
                    icon: 'none',
                    duration: 2000
                  })
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    }
  },
  // 下拉加载
  scroll_get() {
    if (this.scroll_suo) { return false }
    console.log(111)
    var that = this;
    this.setData({
      scroll_true: 'on'
    })
    this.page_num++
    if (that.data.current_type != '3') {
      app.request(app.apiDomain + 'index.php?c=activity&a=detail', {
        activity_id: that.data.activity_id,
        page: that.page_num,
        size: 10
      }, res => {
        this.setData({
          scroll_true: ''
        })
        // console.log(res);
        if (res.data.data.list.length == 0) {
          wx.showToast({
            title: '已经到底了',
            icon: 'none',
            duration: 2000
          })
          that.scroll_suo = true;
        } else {
          that.setData({
            list: that.data.list.concat(res.data.data.list)
          })
        }
      })
    } else {
      app.request(app.apiDomain + 'index.php?c=activity&a=index', {
        page: that.page_num,
        size: 20
      }, res => {
        this.setData({
          scroll_true: ''
        })
        let ending = res.data.data.filter(item => {
          return item.status == 2
        })
        let signing = res.data.data.filter(item => {
          return item.status == 0
        })
        let doing = res.data.data.filter(item => {
          return item.status == 1
        })
        // console.log(res);
        if (ending.length == 0) {
          wx.showToast({
            title: '已经到底了',
            icon: 'none',
            duration: 2000
          })
          that.scroll_suo = true;
        } else {
          that.setData({
            ending: that.data.ending.concat(ending)
          })
        }
      })
    }
  },
  // 计算数字
  countIcon(num) {
    var num1 = num.substring(num.length - 1, num.length);
    var num2 = num.substring(num.length - 2, num.length - 1) ? num.substring(num.length - 2, num.length - 1) : 0;
    var num3 = num.substring(num.length - 3, num.length - 2) ? num.substring(num.length - 3, num.length - 2) : 0;
    var num4 = num.substring(num.length - 4, num.length - 3) ? num.substring(num.length - 4, num.length - 3) : 0;
    return [num1, num2, num3, num4]
  },
  gao_click: function () {
    this.setData({
      gao_on: 'on'
    })
  },
  gao_off: function () {
    this.setData({
      gao_on: ''
    })
  },
  return_back() {
    let pages = getCurrentPages()
    if (pages.length == 1) {
      wx.redirectTo({
        url: '../activityList/index',
      })
    } else {
      wx.navigateBack({

      })
    }
  },
  return_act() {
    wx.redirectTo({
      url: '../index/index',
    })
  },
  onReady() {
    app.SystemInfo(this)
  },
  onShareAppMessage: function () {
    console.log('分享')
    return {
      title: '早起守约,和我一起瓜分健康币吧~',
      path: '/pages/activityList/promise_new?activity_id=' + this.data.activity_id,
      imageUrl: 'https://resource.160nurse.com/canvas/healthy/img/activity/promise_8.png'
    }
  }
})